﻿Public Class 维修结果

#Region "窗体操作"

    '关闭窗体
    Private Sub 维修结果_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        ResuRpForm = Nothing
    End Sub
    '关闭窗体
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Me.Close()
    End Sub

    '窗体加载
    Private Sub 维修结果_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        '加载今天单号
        DH_today()
        '加载未修单号
        DH_NotRP()
        '加载指定日期单号
        DateTimePicker1_ValueChanged(sender, e)

        '显示控件初始化
        MoniStar()

    End Sub
    Private Sub 维修结果_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        RadioButton1.Checked = True
    End Sub

    '加载今天单号
    Private Sub DH_today()

        Dim d1 As Date = Now.Date
        Dim d2 As Date = d1.AddDays(1)
        Dim sql As String = "select 维修单号 from 维修表 where 始修日期 >= #" + d1 + "# and 始修日期 < #" + d2 + "#"
        Dim ds As DataSet = SQLtoDataSet(sql)
        Dim tb As DataTable = ds.Tables(0)
        If tb.Rows.Count > 0 Then
            ComboBox1.Items.Clear()
            For i = 0 To tb.Rows.Count - 1
                ComboBox1.Items.Add(tb.Rows(i).Item(0))
            Next
        Else
            ComboBox1.Items.Clear()
            ComboBox1.Items.Add("今天没有维修单号！")
        End If

    End Sub

    '加载未修单号
    Private Sub DH_NotRP()

        Dim sql As String = "select 维修单号 from 维修表 where 维修结果 is null"
        Dim ds As DataSet = SQLtoDataSet(sql)
        Dim tb As DataTable = ds.Tables(0)
        If tb.Rows.Count > 0 Then
            ComboBox3.Items.Clear()
            For i = 0 To tb.Rows.Count - 1
                ComboBox3.Items.Add(tb.Rows(i).Item(0))
            Next
        Else
            ComboBox3.Items.Clear()
            ComboBox3.Items.Add("没有未结的维修单号！")
        End If

    End Sub

    '加载指定日期单号
    Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged

        Dim d1 As Date = DateTimePicker1.Value.Date
        Dim d2 As Date = d1.AddDays(1)
        Dim sql As String = "select 维修单号 from 维修表 where 始修日期 >= #" + d1 + "# and 始修日期 < #" + d2 + "#"
        Dim ds As DataSet = SQLtoDataSet(sql)
        Dim tb As DataTable = ds.Tables(0)
        ComboBox4.Items.Clear()
        If tb.Rows.Count > 0 Then
            ComboBox4.Items.Clear()
            For i = 0 To tb.Rows.Count - 1
                ComboBox4.Items.Add(tb.Rows(i).Item(0))
            Next
        Else
            ComboBox4.Items.Clear()
            ComboBox4.Items.Add("该天没有维修单号！")
        End If
    End Sub

    '显示控件初始化
    Private Sub MoniStar()
        LB_bsrq.Text = ""
        LB_khdh.Text = ""
        LB_khdz.Text = ""
        LB_ppxh.Text = ""
        LB_sqrq.Text = ""
        LB_ssfy.Text = ""
        LB_wsdh.Text = ""
        LB_wsry.Text = ""
        LB_wsxm.Text = ""
        LB_wszt.Text = ""
        LB_ycfy.Text = ""
        TB_Bz.Text = ""

        RadioButton5.Checked = False
        RadioButton6.Checked = False

        TextBox2.Text = ""

    End Sub

#End Region

#Region "控件选择"

    '选择今天的单号
    Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
        CheckerChange()
    End Sub

    '选择某天的单号
    Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged
        CheckerChange()
    End Sub

    '选择指定的单号
    Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
        CheckerChange()
        TextBox1.Focus()
    End Sub

    '选择未结的单号
    Private Sub RadioButton4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton4.CheckedChanged
        CheckerChange()
    End Sub

    'RadioButton控制使能变化
    Private Sub CheckerChange()
        If RadioButton1.Checked Then
            ComboBox1.Enabled = True
            DateTimePicker1.Enabled = False
            ComboBox4.Enabled = False
            TextBox1.Enabled = False
            Button5.Enabled = False
            ComboBox3.Enabled = False
        ElseIf RadioButton2.Checked Then
            ComboBox1.Enabled = False
            DateTimePicker1.Enabled = False
            ComboBox4.Enabled = False
            TextBox1.Enabled = True
            Button5.Enabled = True
            ComboBox3.Enabled = False
        ElseIf RadioButton3.Checked Then
            ComboBox1.Enabled = False
            DateTimePicker1.Enabled = True
            ComboBox4.Enabled = True
            TextBox1.Enabled = False
            Button5.Enabled = False
            ComboBox3.Enabled = False
        ElseIf RadioButton4.Checked Then
            ComboBox1.Enabled = False
            DateTimePicker1.Enabled = False
            ComboBox4.Enabled = False
            TextBox1.Enabled = False
            Button5.Enabled = False
            ComboBox3.Enabled = True
        End If
    End Sub

#End Region

#Region "数据搜索"

    '单号 to 显示控件
    Private Sub DHtoCtrEr(ByVal DH As String)
        Dim sql As String = "select * from 维修表 where 维修单号 = '" + DH + "'"
        Dim ds As DataSet = SQLtoDataSet(sql)
        Dim dt As DataTable = ds.Tables(0)

        If dt.Rows.Count > 0 Then

            Dim dr As DataRow = dt.Rows(0)

            LB_ppxh.Text = Trim(dr.Item("品牌").ToString) + "  " + Trim(dr.Item("型号").ToString)
            LB_bsrq.Text = dr.Item("始修日期").ToString
            LB_khdh.Text = dr.Item("电话").ToString
            LB_khdz.Text = dr.Item("地址").ToString
            LB_wsdh.Text = dr.Item("维修单号").ToString
            LB_wsry.Text = dr.Item("维修员").ToString
            LB_wsxm.Text = Trim(dr.Item("项目").ToString)

            TextBox3.Text = Trim(dr.Item("修讫备注").ToString)
            TB_Bz.Text = Trim(dr.Item("备注").ToString)

            If dr.Item("修讫日期").ToString = "" Then
                LB_sqrq.Text = "未修讫"
            Else
                LB_sqrq.Text = dr.Item("修讫日期").ToString
            End If

            If Trim(dr.Item("维修结果").ToString) = "" Then
                LB_wszt.Text = "未修讫"
                RadioButton6.Checked = True
            Else
                LB_wszt.Text = dr.Item("维修结果").ToString
                RadioButton5.Checked = True
            End If

            If dr.Item("实收费用").ToString = "" Then
                LB_ssfy.Text = "无记录"
                TextBox2.Text = ""
            ElseIf dr.Item("实收费用").ToString = "-1" Then
                LB_ssfy.Text = "无记录"
                TextBox2.Text = ""
            Else
                LB_ssfy.Text = dr.Item("实收费用").ToString + "元"
                TextBox2.Text = dr.Item("实收费用").ToString
            End If

            If dr.Item("预测费用").ToString = "-1" Then
                LB_ycfy.Text = "无记录"
            Else
                LB_ycfy.Text = dr.Item("预测费用").ToString + "元"
            End If


            TextBox3.Text = dr.Item("修讫备注").ToString


        Else
            MsgBox("找不到维修单号'" + DH + "'的相关信自息！")
            MoniStar()
        End If

    End Sub

    '今天单号
    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        If Trim(ComboBox1.Text) <> "" And Trim(ComboBox1.Text) <> "今天没有维修单号！" Then
            DHtoCtrEr(ComboBox1.Text)
        Else
            MoniStar()
        End If
    End Sub

    '指定单号
    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        If TextBox1.Text <> "" Then
            DHtoCtrEr(TextBox1.Text)
        End If
    End Sub

    '未结单号
    Private Sub ComboBox3_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox3.SelectedIndexChanged
        If Trim(ComboBox3.Text) <> "" And Trim(ComboBox3.Text) <> "没有未结的维修单号！" Then
            DHtoCtrEr(ComboBox3.Text)
        Else
            MoniStar()
        End If

    End Sub

    '某天单号
    Private Sub ComboBox4_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox4.SelectedIndexChanged
        If Trim(ComboBox4.Text) <> "" And Trim(ComboBox4.Text) <> "该天没有维修单号！" Then
            DHtoCtrEr(ComboBox4.Text)
        Else
            MoniStar()
        End If
    End Sub

#End Region

#Region "数据操作"

    '提交按扭
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim sql As String = ""


        If RadioButton5.Checked Then

            '维修完成
            sql += "update 维修表 set 维修结果 = '维修完成', 修讫日期='" + Now.ToString
            If Trim(TextBox2.Text) <> "" Then
                sql += "', 实收费用='" + TextBox2.Text + "'"
            Else
                sql += "', 实收费用=null"
            End If

            If Trim(TextBox3.Text) <> "" Then
                sql += ", 修讫备注='" + TextBox3.Text + "'"
            Else
                sql += ", 修讫备注=null"
            End If

            sql += " where 维修单号 ='" + LB_wsdh.Text + "'"
        Else
            '维修未完成
            sql += "update 维修表 set 维修结果 =null, 修讫日期= null, 实收费用=null, 修讫备注=null where 维修单号 ='" + LB_wsdh.Text + "'"

        End If

        SQLupdata(Sql)
        MsgBox("维修状态修改操作成功！")

        '-----执行一次初始化-------

        '加载未修单号
        DH_NotRP()

        DHtoCtrEr(LB_wsdh.Text)


    End Sub

    '删除按扭
    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Dim r As Microsoft.VisualBasic.MsgBoxResult = MsgBox("你确定你要删除维修单号'" + Trim(LB_wsdh.Text) + "'的记录？", MsgBoxStyle.YesNo, "警告")
        If r = MsgBoxResult.Yes Then
            Dim sql As String = "delete from 维修表 where 维修单号 = '" + Trim(LB_wsdh.Text) + "'"
            SQLupdata(sql)
            MsgBox("已删除维修单号'" + LB_wsdh.Text + "'！")

            '窗体控件重新初始化

            '加载今天单号
            DH_today()
            '加载未修单号
            DH_NotRP()
            '加载指定日期单号
            DateTimePicker1_ValueChanged(sender, e)

            '显示控件初始化
            MoniStar()
        End If


    End Sub

#End Region

    '修讫操作控件变化
    Private Sub RadioButton6_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton6.CheckedChanged
        If RadioButton6.Checked Then
            TextBox2.Enabled = False
            TextBox2.Text = ""
            TextBox3.Enabled = False
            TextBox3.Text = ""
        Else
            TextBox2.Enabled = True
            TextBox2.Focus()
            TextBox3.Enabled = True
            TextBox3.Focus()
        End If
    End Sub

    '只输入数字
    Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
        NumOnly(e)
    End Sub
    '只输入数字
    Private Sub NumOnly(ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If e.KeyChar >= "0" And e.KeyChar <= "9" Or e.KeyChar = Chr(Keys.Back) Or e.KeyChar = Chr(46) Then  '只允许输入数字与点
            e.Handled = False           '当按键不合法时　则不允许输入
        Else
            e.Handled = True            '当按键合法时　则允许输入并显示
        End If
    End Sub



    Dim ZT_title As Font = New Font(New FontFamily("黑体"), 18)         '标题字体
    Dim ZT_Littl As Font = New Font(New FontFamily("宋体"), 12)         '页眉字体

    Dim ZT_phone As Font = New Font(New FontFamily("幼圆"), 10)         '页脚字体1
    Dim ZT_litt2 As Font = New Font(New FontFamily("幼圆"), 8)          '页脚字体2
    Dim ZT_titl2 As Font = New Font(New FontFamily("幼圆"), 15)         '标题字体2

    Dim PrFlag As Boolean       '打印类型标志，true为维修单，false为修讫单

    '打印维修单据
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

        If Trim(LB_wsdh.Text) = "" Then
            MsgBox("请选择维修单号！")
            Exit Sub
        End If

        PrFlag = True

        CType(PrintPreviewDialog1, Form).WindowState = FormWindowState.Maximized
        PrintDocument1.DefaultPageSettings.PaperSize = New Printing.PaperSize(Printing.PaperKind.Custom, 815, 510)

        PrintPreviewDialog1.ShowDialog()

    End Sub
    '打印修讫信息
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        If Trim(LB_wsdh.Text) = "" Then
            MsgBox("请选择维修单号！")
            Exit Sub
        End If

        If Not RadioButton5.Checked Then
            MsgBox("没有维修完成，不能打印修讫核证信息！")
            Exit Sub
        End If

        PrFlag = False

        Dim a As Microsoft.VisualBasic.MsgBoxResult = MsgBox("请打开打印机电源，并放入打印纸！", MsgBoxStyle.OkCancel)

        If a = MsgBoxResult.Cancel Then Exit Sub

        CType(PrintPreviewDialog1, Form).WindowState = FormWindowState.Maximized
        PrintDocument1.DefaultPageSettings.PaperSize = New Printing.PaperSize(Printing.PaperKind.Custom, 815, 510)

        PrintDocument1.Print()

        'PrintPreviewDialog1.ShowDialog()

    End Sub

    '打印事件
    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        Dim g As Graphics = e.Graphics
        Dim pen As Pen = New Pen(Color.Black)
        Dim rect As Rectangle = e.MarginBounds

        If PrFlag Then

            '页眉
            g.DrawLine(Pens.Black, 40, 40, 810 - 50, 40)
            'g.DrawString("买得放心  用得开心", ZT_litt2, Brushes.Black, 40, 23)
            'g.DrawString("伟康电器  信心的保证", ZT_litt2, Brushes.Black, 693 - 50, 23)

            '标题：原设置常化设置，现改出这里的读写设置
            Dim TiS As TitleSet = ReadTx("REP.txt")
            g.DrawString(TiS.T1, ZT_title, Brushes.Black, TiS.T1x, TiS.T1y)
            g.DrawString(TiS.T2, ZT_title, Brushes.Black, TiS.T2x, TiS.T2y)
            g.DrawString(TiS.T3, ZT_title, Brushes.Black, TiS.T3x, TiS.T3y)

            '标题：原设置常化设置，现改出上面的读写设置
            'g.DrawString("东 莞 市 虎 门 伟 康 冷 冻 设 备", ZT_title, Brushes.Black, 213 - 50, 50)
            'g.DrawString("东 莞 市 虎 门 绍 康 电 器 商 店", ZT_title, Brushes.Black, 213 - 50, 95)
            'g.DrawString("商 品 销 售 单", ZT_title, Brushes.Black, 325 - 50 + 3, 135)

            '电话、电址
            g.DrawString("电话：85511208  85512003", ZT_phone, Brushes.Black, 220, 510 - 50)
            g.DrawString("地址：虎门执信路43号", ZT_phone, Brushes.Black, 40, 510 - 50)

            '打印日期、出单日期
            g.DrawString("打印时间：" + Now.ToString, ZT_litt2, Brushes.Black, 650 - 50, 515 - 50)
            g.DrawString("日期：" + Now.ToString, ZT_phone, Brushes.Black, 640 - 50, 185)

            '客户、地址
            g.DrawString("客户：" + LB_khdz.Text, ZT_phone, Brushes.Black, 42, 160)
            g.DrawString("联系：" + LB_khdh.Text, ZT_phone, Brushes.Black, 42, 185)

            '单号：
            g.DrawString(LB_wsdh.Text, ZT_titl2, Brushes.Black, 640 - 50, 157)

            '表格
            DrawTable(g, pen, rect)

            '表头
            g.DrawString("品 名 规 格", ZT_phone, Brushes.Black, 100 - 25, 220)
            g.DrawString("修 讫 核 证 信 息", ZT_phone, Brushes.Black, 100 - 25, 333)

            g.DrawString("维修人员", ZT_phone, Brushes.Black, 340 - 60, 220)
            g.DrawString("维  修  项  目", ZT_phone, Brushes.Black, 467 - 30, 220)
            g.DrawString("预测费用", ZT_phone, Brushes.Black, 721 - 55, 220)


            '填表
            DrawList(g, pen, rect)

        Else


            '修讫核证行
            PrEnd(g, pen, rect)

        End If

    End Sub
    '画表
    Private Sub DrawTable(ByVal g As Graphics, ByVal pen As Pen, ByVal rect As Rectangle)

        '竖边
        g.DrawLine(Pens.Black, 41, 217, 41, 322)
        g.DrawLine(Pens.Black, 41, 330, 41, 496 - 60)

        g.DrawLine(Pens.Black, 809 - 50, 217, 809 - 50, 322)
        g.DrawLine(Pens.Black, 809 - 50, 330, 809 - 50, 496 - 60)

        '横边
        g.DrawLine(Pens.Black, 40, 213, 810 - 50, 213)
        g.DrawLine(Pens.Black, 40, 500 - 60, 810 - 50, 500 - 60)


        '内横
        For i = 1 To 2
            g.DrawLine(Pens.Black, 48, 211 + (30 * i), 802 - 50, 211 + (30 * i))
        Next

        '中横
        g.DrawLine(Pens.Black, 40, 211 + (30 * 3) + 25, 810 - 50, 211 + (30 * 3) + 25)

        '后横
        g.DrawLine(Pens.Black, 48, 211 + (30 * 4) + 25, 802 - 50, 211 + (30 * 4) + 25)

        '内竖--中线
        g.DrawLine(Pens.Black, 425 - 50, 217, 425 - 50, 267)

        '内竖--后三分显一
        g.DrawLine(Pens.Black, 681 - 50, 217, 681 - 50, 267)

        '内竖--前三份显一
        g.DrawLine(Pens.Black, 296 - 50, 217, 296 - 50, 267)
        g.DrawLine(Pens.Black, 296 - 50, 330, 296 - 50, 352)

    End Sub
    '填表
    Private Sub DrawList(ByVal g As Graphics, ByVal pen As Pen, ByVal rect As Rectangle)
        Dim y As Integer = 248

        '品牌型号
        g.DrawString(LB_ppxh.Text, ZT_phone, Brushes.Black, 75, y)
        '维修人员
        g.DrawString(LB_wsry.Text, ZT_phone, Brushes.Black, 280, y)
        '维修项目
        g.DrawString(LB_wsxm.Text, ZT_phone, Brushes.Black, 437, y)

        '预测费用
        If LB_ycfy.Text = "无记录" Then
            g.DrawString("未确定", ZT_phone, Brushes.Black, 666, y)

        Else
            g.DrawString("￥" + LB_ycfy.Text, ZT_phone, Brushes.Black, 666, y)
        End If

        If Trim(TB_Bz.Text) <> "" Then
            g.DrawString("备注：" + TB_Bz.Text, ZT_phone, Brushes.Black, 75, 289)
        End If

    End Sub
    '修讫核证行
    Private Sub PrEnd(ByVal g As Graphics, ByVal pen As Pen, ByVal rect As Rectangle)

        If TextBox2.Text <> "" Then
            g.DrawString("维修完成！  实际收取费用：￥" + TextBox2.Text + "元", ZT_phone, Brushes.Black, 75, 373)
        Else
            g.DrawString("维修完成！", ZT_phone, Brushes.Black, 75, 373)
        End If

        If TextBox3.Text <> "" Then
            g.DrawString("备注：" + TextBox3.Text, ZT_phone, Brushes.Black, 75, 407)
        End If


    End Sub

End Class